package com.ny.service.impl;

import com.ny.entity.Devices;
import com.ny.mapper.DeviceMapper;
import com.ny.service.DeviceService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Optional;

@Service
public class DeviceServiceImpl implements DeviceService {

    @Autowired
    private DeviceMapper deviceMapper;


    @Override
    public List<Devices> findAll() {
        return deviceMapper.findAll();
    }

    @Override
    public Devices addDevice(Devices device) {
        return deviceMapper.save(device);
    }

    @Override
    public Devices updateDevice(int device_id, Devices device) {
        Optional<Devices> existingDevice = deviceMapper.findById(device_id);
        if (existingDevice.isPresent()) {
            device.setId(existingDevice.get().getId());
            device.setDevice_id(device_id);
            return deviceMapper.save(device);
        }
        return null;
    }

    @Override
    public void deleteDevice(int device_id) {
        deviceMapper.deleteById(device_id);
    }

    @Override
    public List<Devices> findDevicesByName(String name) {
        return deviceMapper.findByName(name);
    }
}